package org.wattdepot.cli;

import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.resource.property.jaxb.Property;
import org.wattdepot.resource.sensordata.jaxb.SensorData;
import org.wattdepot.util.tstamp.Tstamp;
/**
 * Lists sensor data for day and timestamp values.
 * @author Aaron Herres
 *
 */
public class ListSensorData implements SingleCommand {

  //Static strings for error messages. Eventually refactor into a global array.
  /**
   * Essentially the main() of the sensor listing class.
   * @author Aaron Herres
   * @param client The WattDepotClient to use in connections.
   * @param args The argument string.
   * @return the output message.
   */
  public String processCommand(WattDepotClient client, String[] args) {
    if (!client.isHealthy()) {
      return Messages.Error.CLIENT_NOT_READY;
    }
    XMLGregorianCalendar start = null;
    XMLGregorianCalendar end = null;
    String source = args[2];
      try {

        start = Tstamp.makeTimestamp(args[4]);
        if (args[3].equals("DAY") ) {
          end = Tstamp.incrementDays(start, 1);
        }
      }
      catch (Exception e) {
        return Messages.Error.INVALID_INPUT;
      }

    try {
      if (end == null) {  
        return this.toCLIOutput(client.getSensorData(source, start));
      }
      else {
        return this.toCLIOutput(client.getSensorDatas(source, start, end));
      }
        
    }
    catch (Exception e) {
      return Messages.Error.INVALID_TIMESTAMP;
    }
  }
  /**
   * Produces a string message for command-line output for an array of sensordatas.
   * @author Aaron Herres
   * @param sensorDatas An array of sensor datas to produce messages for.
   * @return The human-readable message.
   */
  private String toCLIOutput(List<SensorData> sensorDatas) {
    StringBuffer message = new StringBuffer(50);
    for (SensorData s : sensorDatas) {
      message.append(s.getTimestamp().toString());
      message.append('\t');
      message.append(toCLIOutput(s).replaceAll("\n", " "));
      message.append('\n');
    }
    return message.toString();
  }
  /**
   * Produces a human-readable string representing a single sensor data.
   * @author Aaron Herres
   * @param cd The sensor data to create a message for.
   * @return The human-readable message.
   */
  private String toCLIOutput(SensorData cd) {
    StringBuffer message = new StringBuffer(50);
    message.append("Tool: ");
    message.append(cd.getTool());
    message.append("\nSource: ");
    message.append(cd.getSource());
    message.append("\n Properties: ");
    for (Property p : cd.getProperties().getProperty()) {
      message.append('(');
      message.append(p.getKey());
      message.append(" : ");
      message.append(p.getValue());
      message.append(')');
    }
    message.append('\n');
    return message.toString();
  }
  @Override
  public String usageString() {
    // TODO Auto-generated method stub
    return "Usage: sensordata {source} [ timestamp {timestamp} | day {day}";
  }

}
